package com.example.mydemo.controller.hc;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mydemo.entity.Users;
import com.example.mydemo.entity.domain.ResponseDTO;
import com.example.mydemo.entity.domain.UserRoleVO;
import com.example.mydemo.model.DepVO;
import com.example.mydemo.model.tableVO;
import com.example.mydemo.service.IUserRoleService;
import com.example.mydemo.service.UserService;
import com.example.mydemo.util.RedisUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author syf
 * @since 2025-05-28
 */
@RestController
@RequestMapping("/userrole")
public class UserRoleController {
    @Autowired
    private UserService userService;
    @Autowired
    private IUserRoleService IUserRoleService;
    @Autowired
    private RedisUtil redisUtil;
    @GetMapping("/getUserRolepage")
    @ResponseBody
    public tableVO selectDeptCount(@RequestParam Long page, @RequestParam Long limit, @RequestParam(defaultValue = "") String searchParams) {
        Page<UserRoleVO> deppage = new Page<>(page, limit); // 创建分页对象，当前页为1，每页显示5条记录
        List<UserRoleVO> b = new ArrayList<>();
        if (searchParams == "") {
            b = IUserRoleService.selectName();
        } else {
            b = IUserRoleService.selectByUserName(searchParams);
        }
        tableVO a = new tableVO();
        a.setCode(0L);
        a.setMsg("");
        a.setData(b);
        a.setCount((long) b.size());
        return a;
    }
    @PostMapping("/addUserRole")
    public ResponseDTO<UserRoleVO> getUsers2(@RequestParam("userId") String userId , @RequestParam("roleId") String roleId) {
        UserRoleVO userRoleVO = new UserRoleVO();
        userRoleVO.setUserId(Long.valueOf(userId));
        userRoleVO.setRoleId(Long.valueOf(roleId));
        userRoleVO.setCreateBy("syf");
        userRoleVO.setCreateTime( LocalDateTime.now());
        IUserRoleService.saveUser(userRoleVO);
        return new ResponseDTO<UserRoleVO>(200, "添加信息成功！", userRoleVO);
    }
    @PostMapping("/userRoleDelete")
    @ResponseBody
    public ResponseDTO<Users> delete(@RequestParam Long id) {
        IUserRoleService.delete(id);
        return new ResponseDTO<>(200, "删除信息成功！", null);
    }
}
